package cn.mes.module.mes.service.baseworkshop;

import cn.mes.module.mes.controller.admin.basemachineinfo.vo.BaseMachineInfoPageReqVO;
import cn.mes.module.mes.dal.mysql.basemachineinfo.BaseMachineInfoMapper;
import cn.mes.module.system.service.tenant.TenantService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.mes.module.mes.controller.admin.baseworkshop.vo.*;
import cn.mes.module.mes.dal.dataobject.baseworkshop.BaseWorkShopDO;
import cn.mes.module.mes.dal.dataobject.basemachineinfo.BaseMachineInfoDO;
import cn.mes.module.mes.dal.dataobject.baseworkshop.BaseMachineAreaDO;
import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.pojo.PageParam;
import cn.mes.framework.common.util.object.BeanUtils;

import cn.mes.module.mes.dal.mysql.baseworkshop.BaseWorkShopMapper;
import cn.mes.module.mes.dal.mysql.baseworkshop.BaseMachineAreaMapper;

import static cn.mes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mes.module.mes.enums.ErrorCodeConstants.*;

/**
 * 车间信息 Service 实现类
 *
 * @author 游创
 */
@Service
@Validated
public class BaseWorkShopServiceImpl implements BaseWorkShopService {

    @Resource
    private BaseWorkShopMapper baseWorkShopMapper;
    @Resource
    private BaseMachineInfoMapper baseMachineInfoMapper;
    @Resource
    private BaseMachineAreaMapper baseMachineAreaMapper;
    @Resource
    @Lazy // 延迟，避免循环依赖报错
    private TenantService tenantService;

    @Override
    public String createBaseWorkShop(BaseWorkShopSaveReqVO createReqVO) {
        // 插入
        BaseWorkShopDO baseWorkShop = BeanUtils.toBean(createReqVO, BaseWorkShopDO.class);
        baseWorkShopMapper.insert(baseWorkShop);
        // 返回
        return baseWorkShop.getId();
    }

    @Override
    public void updateBaseWorkShop(BaseWorkShopSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseWorkShopExists(updateReqVO.getId());
        // 更新
        BaseWorkShopDO updateObj = BeanUtils.toBean(updateReqVO, BaseWorkShopDO.class);
        baseWorkShopMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBaseWorkShop(String id) {
        // 校验存在
        validateBaseWorkShopExists(id);
        // 删除
        baseWorkShopMapper.deleteById(id);

        // 删除子表
        deleteBaseMachineInfoByWorkShopId(id);
        deleteBaseMachineAreaByWorkShopId(id);
    }

    private void validateBaseWorkShopExists(String id) {
        if (baseWorkShopMapper.selectById(id) == null) {
            throw exception(BASE_WORK_SHOP_NOT_EXISTS);
        }
    }

    @Override
    public BaseWorkShopDO getBaseWorkShop(String id) {
        return baseWorkShopMapper.selectById(id);
    }


    @Override
    public List<BaseWorkShopDO> getWorkShopList() {
        return baseWorkShopMapper.selectList();
    }


    @Override
    public List<BaseWorkShopDO> getWorkShopList(WorkShopListReqVO reqVO) {
        return baseWorkShopMapper.selectList(reqVO);
    }

    @Override
    public List<BaseWorkShopDO> getWorkShopListByTenant(WorkShopListReqVO reqVO) {
        // 查询所有车间，并过滤掉关闭的节点
        return getWorkShopList(reqVO);
    }




    @Override
    public PageResult<BaseWorkShopDO> getBaseWorkShopPage(BaseWorkShopPageReqVO pageReqVO) {
        return baseWorkShopMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（生产区域信息） ====================

    @Override
    public PageResult<BaseMachineAreaDO> getBaseMachineAreaPage(PageParam pageReqVO, String workShopId) {
        return baseMachineAreaMapper.selectPage(pageReqVO, workShopId);
    }


    @Override
    public List<BaseMachineAreaDO> getMachineAreaList(MachineAreaListReqVO reqVO) {
        return baseMachineAreaMapper.selectList(reqVO);
    }

    @Override
    public String createBaseMachineArea(BaseMachineAreaDO baseMachineArea) {
        baseMachineAreaMapper.insert(baseMachineArea);
        return baseMachineArea.getId();
    }

    @Override
    public void updateBaseMachineArea(BaseMachineAreaDO baseMachineArea) {
        // 校验存在
        validateBaseMachineAreaExists(baseMachineArea.getId());
        // 更新
        baseMachineAreaMapper.updateById(baseMachineArea);
    }

    @Override
    public void deleteBaseMachineArea(String id) {
        // 校验存在
        validateBaseMachineAreaExists(id);
        // 删除
        baseMachineAreaMapper.deleteById(id);
    }

    @Override
    public BaseMachineAreaDO getBaseMachineArea(String id) {
        return baseMachineAreaMapper.selectById(id);
    }

    private void validateBaseMachineAreaExists(String id) {
        if (baseMachineAreaMapper.selectById(id) == null) {
            throw exception(BASE_MACHINE_AREA_NOT_EXISTS);
        }
    }

    private void deleteBaseMachineAreaByWorkShopId(String workShopId) {
        baseMachineAreaMapper.deleteByWorkShopId(workShopId);
    }

    // ==================== 子表（机台产线基础信息） ====================

    @Override
    public PageResult<cn.mes.module.mes.dal.dataobject.basemachineinfo.BaseMachineInfoDO> getBaseMachineInfoPage(BaseMachineInfoPageReqVO pageReqVO) {
        return baseMachineInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public String createBaseMachineInfo(BaseMachineInfoDO baseMachineInfo) {
        baseMachineInfoMapper.insert(baseMachineInfo);
        return baseMachineInfo.getId();
    }

    @Override
    public void updateBaseMachineInfo(BaseMachineInfoDO baseMachineInfo) {
        // 校验存在
        validateBaseMachineInfoExists(baseMachineInfo.getId());
        // 更新
        baseMachineInfoMapper.updateById(baseMachineInfo);
    }

    @Override
    public void deleteBaseMachineInfo(String id) {
        // 校验存在
        validateBaseMachineInfoExists(id);
        // 删除
        baseMachineInfoMapper.deleteById(id);
    }

    @Override
    public BaseMachineInfoDO getBaseMachineInfo(String id) {
        return baseMachineInfoMapper.selectById(id);
    }

    private void validateBaseMachineInfoExists(String id) {
        if (baseMachineInfoMapper.selectById(id) == null) {
            throw exception(BASE_MACHINE_INFO_NOT_EXISTS);
        }
    }

    private void deleteBaseMachineInfoByWorkShopId(String workShopId) {
        baseMachineInfoMapper.deleteByWorkShopId(workShopId);
    }


}